<div class="dealOrders index">
<?php if(!empty($deal)) {?>
	<div class="actions">
		<ul>
			<li>
				<?php
				echo $html->link(__('Deals Index'), array("plugin"=>"deals",'controller'=>'deals','action'=>'admin_index'),array('class'=>'add_button'));
				?>
			</li>
		</ul>
	</div>
<?php } ?>
<?php
$index->setData($dealOrders, 'DealOrder');

$index->showFields(array("id",array('deal_id'=>__('Deal')),array('user_id'=>__('User')),"quantity",array('created'=>__('Purchased On')),array('amount'=>__('Amount')),array('paymentTransactionId'=>__('Transaction ID')),array('status2'=>__('Status'))));
//$index->sortableFields(array());

$index->addColumn('user_id','showUser');
$index->addColumn('deal_id','showDeal');
$index->addColumn('status2','showStatus');

function showDeal($record)
{
	return $record['Deal']['title'];
}

function showUser($record)
{
	return $record['User']['display_name'];
}

function showStatus($record)
{
	if($record['DealOrder']['status'] == DealOrder::STATUS_FAILED)
	{
		return __("Failed");
	} else if ($record['DealOrder']['status'] == DealOrder::STATUS_COMPLETE) {
		return __("Completed");
	} else {
		return __("Pending");
	}
}

function canMarkCompleted($model,$record)
{
	//@todo - confirm with shrey for other payment related statuses
	if($record[$model]['status'] == DealOrder::STATUS_PENDING)
	{
		$action['title'] = __('Set as completed');
		$action['url'] = '/admin/deals/deal_orders/changeStatus/'.$record[$model]['id'];
		$action['confirmMessage'] = __('Are you sure ?');
	} else {
		$action = false;
	}
	return $action;
}

function canMarkUsedAll($model, $record)
{
	if(!$record[$model]['is_used'])
	{
		$action['title'] = __('Mark all used');
		$action['url'] = '/admin/deals/deal_orders/changeUsedStatus/'.$record[$model]['id'];
		$action['confirmMessage'] = __('Are you sure ?');
		return $action;
	} else {
		return false;
	}
}

$index->setAction(__('View Details'), array("plugin"=>"deals","controller"=>"deal_order_codes",'action'=>'index', ':DealOrder.id'));
$index->setAction(null, null,$this->helpers,false,true,'canMarkCompleted');
$index->setAction(null, null,$this->helpers,false,true,'canMarkUsedAll');

echo $index->display($dealOrders);
?>
</div>